package com.eeepay.eeepay_v2.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.util.ABAppUtil;

/**
 * Created by Ching on 2017/2/28.
 * 新增代理商 分润方式选择Dialog
 */

public class ProfitDialog extends Dialog implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private Context mContext;

    private RadioGroup radioGroup;
    private EditText editText;
    private Button button;
    private ImageView cancleIv;

    private String type;

    private OnConfirmListener onClickListener;

    public ProfitDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_profit_setting);
        radioGroup = (RadioGroup) findViewById(R.id.rgp_profit_dialog);
        editText = (EditText) findViewById(R.id.et_profit_dialog);
        button = (Button) findViewById(R.id.btn_confirm);
        cancleIv = (ImageView) findViewById(R.id.iv_cancle);

        radioGroup.setOnCheckedChangeListener(this);
        button.setOnClickListener(this);
        cancleIv.setOnClickListener(this);
        radioGroup.getChildAt(0).performClick();
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2)
                {
                    s.delete(posDot + 3, posDot + 4);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cancle:
                ABAppUtil.hideSoftInput(mContext);
                super.dismiss();
                break;
            case R.id.btn_confirm:
                if (TextUtils.isEmpty(type) || TextUtils.isEmpty(editText.getText().toString().trim())) {
                    Toast.makeText(mContext, "请完成填写", Toast.LENGTH_SHORT).show();
                    return;
                }
                ABAppUtil.hideSoftInput(mContext,editText);
                onClickListener.onConfirm();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rbtn_fixed_amount://固定金额
                type = Constant.PROFIT_TYPE_1;
                break;
            case R.id.rbtn_earning_ratio://收益比率
                type = Constant.PROFIT_TYPE_2;
                break;
            case R.id.rbtn_agency_cost://代理成本
                type = Constant.PROFIT_TYPE_3;
                break;
        }
    }

    public String getProfitType() {
        return type;
    }

    public void setProfitType(String type){
        switch (type){
            case Constant.PROFIT_TYPE_1:
                radioGroup.getChildAt(0).performClick();
                break;
            case Constant.PROFIT_TYPE_2:
                radioGroup.getChildAt(1).performClick();
                break;
            case Constant.PROFIT_TYPE_3:
                radioGroup.getChildAt(2).performClick();
                break;
        }
    }


    public String getPercent() {
        return editText.getText().toString().trim();
    }

    public interface OnConfirmListener {
        void onConfirm();
    }

    public void setConfirmListener(OnConfirmListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
